import config from '@/common/config/default.js'
import sign from '@/common/lib/sign.js'
import dry from '@/common/lib/dry.js'
var request = {};

request.run = function(name,token)
{
	var appKey = config.appKey;
	var domain = config.domain;
	var list = config.list;
	var item = list[name];
	if(item.domain == ""){
		item.domain = domain;
	}
	var url = item.domain + item.url;
	var data = {
		"signUrl": item.url
	};
	var header = {};
	if(item.method == "GET" || item.method == "DELETE") {
		header = {
			"content-type": "application/x-www-form-urlencoded"
		};
	} else {
		header = {
			"content-type": "application/json"
		};
	}
	data = sign.sign(appKey,data);
	header.token = token;
	header.sign = data.sign;
	header.rand = data.rand;
	header.timestamp = data.timestamp;
	var getPostData = {};
	getPostData.url = url;
	getPostData.method = item.method;
	getPostData.header = header;
	return getPostData;
};

request.addOrEdit = function(apiName,data,token,isEdit) {
	var requestData = request.run(apiName,token);
	uni.request({
		url: requestData.url,
		method: requestData.method,
		header: requestData.header,
		data: data,
		success: data => {
			if(data.statusCode == 200 && data.data.code == 0){
				if(isEdit) {
					uni.showToast({
						title: "更新成功",
						duration: 1000
					});
				} else {
					uni.showToast({
						title: "新增成功",
						duration: 1000
					});
				}
			}
		}
	})
}

request.delete = function(apiName,data,token){
	var requestData = request.run(apiName,token);
	uni.request({
		url: requestData.url,
		method: requestData.method,
		header: requestData.header,
		data: data,
		success: data => {
			if(data.statusCode == 200 && data.data.code == 0){
				uni.showToast({
					title: "删除成功",
					duration: 1000
				});
			}
		}
	})
}    

module.exports = request;